package src.edu.wayne.cs.csc4110.hw6.implementations.userPanels;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import src.edu.wayne.cs.csc4110.hw6.implementations.stateMachine.StateMachine;
import src.edu.wayne.cs.csc4110.hw6.implementations.stateMachine.StateMachineFrame;
import src.edu.wayne.cs.csc4110.hw6.interfaces.User;

public class UserLoginPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3873968149804844746L;
	
	StateMachine stateMachine;
	JTextField jtfEmail = new JTextField();
	JTextField jtfUserName = new JTextField();
	JPasswordField jtpfUserPassword = new JPasswordField();
	JButton jbtnSubmit = new JButton("Login");
	JButton jbtnRegister = new JButton("Register New User");
	String strEmail = new String();
	String strUserPassword = new String();
	
	public UserLoginPanel(StateMachine stateMachine){
		this.stateMachine = stateMachine;

		//Create a panel
		this.setLayout(new BorderLayout(0, 175));
		this.setBackground(StateMachineFrame.getColor());
		this.setForeground(StateMachineFrame.getColor());
		this.add(new JLabel("Welcome to Facelook.  Please Login"), BorderLayout.NORTH);
		
		
		//Put buttons on it
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,0));
		buttonPanel.setBackground(StateMachineFrame.getColor());
	

		jbtnRegister.addActionListener(
    			new ActionListener() 
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					goToRegisterState();
    				}
    			}
    		);
   	 
		jbtnSubmit.addActionListener(
    			new ActionListener() 
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					if (checkLogin())
    					{
    						goToLoggedInState();
    					}
    					else
    					{
    						JOptionPane.showMessageDialog(null, "Login Failed. Please Try Again");
    					}
    				}
    			}
    		);    	
		
		buttonPanel.add(jbtnRegister);
		buttonPanel.add(jbtnSubmit);
		this.add(buttonPanel, BorderLayout.SOUTH);

		//Put text boxes and labels on it
    		JPanel emailTextField = new JPanel();
    		emailTextField.setLayout(new GridLayout(1,0,2,2));
    		emailTextField.add(new JLabel("Email Address:"));
    		emailTextField.add(jtfEmail);
    
    		jtfEmail.addFocusListener(

    			new FocusListener()
    			{
    				@Override
    				public void focusLost(FocusEvent e)
    				{
    					strEmail = jtfEmail.getText();
    				}
	
					@Override
					public void focusGained(FocusEvent arg0) {
						
					}
    			}
    		);
    		
    		jtfEmail.addActionListener(
    			new ActionListener() 
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					strEmail = jtfEmail.getText();
    				}
    			}
    		);
		
    		JPanel userPassWordTextField = new JPanel();
    		userPassWordTextField.setLayout(new GridLayout(1,0,2,2));
    		userPassWordTextField.add(new JLabel("Password:"));
    		userPassWordTextField.add(jtpfUserPassword);
    
    		jtpfUserPassword.addFocusListener(

    			new FocusListener()
    			{
					@Override
    				public void focusLost(FocusEvent e)
    				{
    					strUserPassword = new String(jtpfUserPassword.getPassword());
    				}
	
					@Override
					public void focusGained(FocusEvent arg0) {
						
					}
    			}
    		);
    		
    		jtpfUserPassword.addActionListener(
    			new ActionListener() 
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					strUserPassword = new String(jtpfUserPassword.getPassword());
    				}
    			}
    		);
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(0,1,2,2));
		centerPanel.setBackground(StateMachineFrame.getColor());
		centerPanel.add( emailTextField );
		
		centerPanel.add( userPassWordTextField );
		
		this.add(centerPanel, BorderLayout.CENTER);

		stateMachine.setDisplayPanel(this, "Login");
	}
	
	private boolean checkLogin()
	{
		// TODO this needs to be cleaned up via delegation.
		boolean result;
		result = stateMachine.getFrameWork().getDataBase().login(strEmail, strUserPassword);
	
		if (result)
		{
			User user = stateMachine.getFrameWork().getDataBase().getUser(strEmail);
			stateMachine.getFrameWork().addUserToSession(user);
		}
		return result;
	}
	
	private void goToRegisterState() {
		stateMachine.goToRegisterState();
	}
	
	private void goToLoggedInState(){
		stateMachine.setState(stateMachine.getLoggedInState());
		stateMachine.goToLoggedInState();
	}
}
